package vip.xiaonuo.modular.reactor;

import cn.hutool.core.util.ObjectUtil;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import reactor.core.Reactor;
import reactor.event.Event;
import reactor.function.Consumer;
import vip.xiaonuo.core.exception.ServiceException;
import vip.xiaonuo.facedoor.modular.topic.EventTopic;
import vip.xiaonuo.facedoor.modular.visitor.entity.Visitor;
import vip.xiaonuo.facedoor.modular.visitor.service.VisitorService;
import vip.xiaonuo.modular.remoteapi.stairs.RemoteStairsConstant;
import vip.xiaonuo.modular.sync.DeviceEvent;
import vip.xiaonuo.modular.ybuildingvisitorrl.entity.YBuildingVisitorRl;
import vip.xiaonuo.modular.ybuildingvisitorrl.mapper.YBuildingVisitorRlMapper;
import vip.xiaonuo.modular.ybuildingvisitorrl.param.YBuildingVisitorRlParam;
import vip.xiaonuo.modular.ydeviceevent.entity.YDeviceEvent;
import vip.xiaonuo.modular.ydeviceevent.mapper.YDeviceEventMapper;
import vip.xiaonuo.modular.yelevatordevice.entity.YElevatorDevice;
import vip.xiaonuo.modular.yelevatordevice.service.YElevatorDeviceService;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

import static reactor.event.selector.Selectors.$;


/**
 * 梯控监听访客删除事件做人员变更
 */
@Service
public class StarisVisitorDeleteListener implements Consumer<Event<Visitor>>, InitializingBean {

  @Resource
  private YBuildingVisitorRlMapper yBuildingVisitorRlMapper;

  @Autowired
  private YElevatorDeviceService elevatorDeviceService;

  @Resource
  private YDeviceEventMapper deviceEventEntityMapper;

  @Autowired
  private Reactor reactor;

  @Override
  public void accept(Event<Visitor> event) {

    Visitor visitor = event.getData();
    if (ObjectUtil.equal(visitor.getSource(),1)||ObjectUtil.equal(visitor.getSource(),2)){
      yBuildingVisitorRlMapper.deleteByVisitorId(visitor.getId());
      return;
    }
    List<YBuildingVisitorRl> list = yBuildingVisitorRlMapper.findList(new YBuildingVisitorRlParam().setVisitorId(visitor.getId()));
    if (CollectionUtils.isNotEmpty(list)) {
      for (YBuildingVisitorRl yBuildingVisitorRl : list) {
        yBuildingVisitorRl.setDeviceSyncStatus(RemoteStairsConstant.DeviceSyncStatus.DELETE);
        yBuildingVisitorRlMapper.updateById(yBuildingVisitorRl);
        // 存在任务表
        YElevatorDevice device = elevatorDeviceService.getById(yBuildingVisitorRl.getDeviceId());
        YDeviceEvent deviceEvent = new YDeviceEvent();
        deviceEvent.setDeviceNo(device.getDeviceNo());
        deviceEvent.setType(RemoteStairsConstant.RequestCmd.DELETE_PERSON);
        deviceEvent.setCreateDate(new Date());
        deviceEvent.setSysType(2);
        deviceEvent.setBody(yBuildingVisitorRl.getVisitorId() + ":" + yBuildingVisitorRl.getVisitorId());
        deviceEvent.setStatus(DeviceEvent.NEW);
        deviceEventEntityMapper.insert(deviceEvent);
      }
    }
  }

  @Override
  public void afterPropertiesSet() throws Exception {
    reactor.on($(EventTopic.VISITOR_DELETE), this);
  }
}
